package com.wpi.iCoach;

import java.util.List;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * This class would display the current location of user and 
 * the location of selected services. Also, users can get the 
 * direction between this two locations and add visiting information 
 * for this selected services.
 * @author Kenneth J. Loomis (kjloomis),
 *         Kevin Chen(xchen)
 * @version June 2012
 */
public class LocationView extends MapActivity implements OnClickListener {

	/**
	 * A View which displays a map (with data obtained from the Google Maps service).
	 */
	private MapView mv;
	
	/**
	 * A utility class to manage panning and zooming of a map.
	 */
	private MapController mc;
	
	/**
	 * A class contains all the information of the selected services and current location of user 
	 */
	private CurrentMap loc;
	
	/**
	 * These views would be display on the screen to show 
	 * the information of the selected services. 
	 */
	private TextView tvName;
	private TextView tvVisits;
	private TextView tvDist;
	private RatingBar rbRating;
	
	/**
	 * This button is for user to get the direction.
	 */
	private Button btnGoMap;
	
	/**
	 * This button is for user to add visiting information.
	 */
	private Button btnUpdate;
	
    /**
     * This class extends Overlay which would be displayed 
     * on top of map from the Google Maps service with the 
     * pins and names of current location of user and the 
     * location of selected services.   
     * @author Ken
     *
     */
	class MapOverlay extends Overlay
	{

		@Override
		public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
			// TODO Auto-generated method stub
			super.draw(canvas, mapView, shadow, when);
			
			Point pt1 = new Point ( );
			mapView.getProjection( ).toPixels( loc.getGeoPointThere(), pt1 );
			
			Point pt2 = new Point ( );
			mapView.getProjection( ).toPixels( loc.getGeoPointHere(), pt2 );
			
			
		    Paint strokePaint = new Paint();
		    strokePaint.setARGB(255, 0, 0, 0);
		    strokePaint.setTextAlign(Paint.Align.CENTER);
		    strokePaint.setTextSize(20);
		    strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
		    strokePaint.setStyle(Paint.Style.STROKE);
		    strokePaint.setStrokeWidth(1);
		    strokePaint.setAntiAlias(true);

		    Paint textPaint1 = new Paint();
		    textPaint1.setColor( Color.RED );
		    textPaint1.setTextAlign(Paint.Align.CENTER);
		    textPaint1.setTextSize(20);
		    textPaint1.setTypeface(Typeface.DEFAULT_BOLD);
		    textPaint1.setAntiAlias(true);

			Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor( Color.BLACK );
			canvas.drawCircle( pt1.x, pt1.y, 10, paint );
			paint.setColor( Color.RED );
			canvas.drawCircle( pt1.x, pt1.y, 9, paint );
			
		    //canvas.drawText( CurrentMap.name,  pt1.x, pt1.y-15, strokePaint );
		    //canvas.drawText( CurrentMap.name,  pt1.x, pt1.y-15, textPaint1 );
			//Kevin's new code
		    canvas.drawText( loc.name,  pt1.x, pt1.y-15, strokePaint );
		    canvas.drawText( loc.name,  pt1.x, pt1.y-15, textPaint1 );
		    
		    Paint textPaint2 = new Paint();
		    textPaint2.setColor( Color.BLUE );
		    textPaint2.setTextAlign(Paint.Align.CENTER);
		    textPaint2.setTextSize(20);
		    textPaint2.setTypeface(Typeface.DEFAULT_BOLD);
		    textPaint2.setAntiAlias(true);
		    
			Paint paint2 = new Paint();
            paint2.setAntiAlias(true);
            paint2.setColor( Color.BLACK );
			canvas.drawCircle( pt2.x, pt2.y, 10, paint2 );
            paint2.setColor( Color.BLUE );
			canvas.drawCircle( pt2.x, pt2.y, 10, paint2 );

		    canvas.drawText( "You are here",  pt2.x, pt2.y-15, strokePaint );
		    canvas.drawText( "You are here",  pt2.x, pt2.y-15, textPaint2 );
			
			return true;
		}
		
	}
	
		
	/**
	 * This method would be called when this activity is first created.
	 * It would first load its UI component from the location_view.xml 
	 * and display the information of selected service. It also display a 
	 * map with the current location of user and the location of selected service.
	 * Besides, user can get the direction and add visiting information on this display.
	 * @param saveInstanceState
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView ( R.layout.location_view );
		
		//loc = CurrentMap.getMap ( );
				//Kevin's new code
				String[] location = new String[8];
				String info = "";
				Bundle extras = getIntent().getExtras();
				location = extras.getStringArray("location");
				loc = new CurrentMap();
				
				String r = location[3];
				float rating = Float.parseFloat(r);
				
				String tLa = location[4];
				double thereLa = Double.parseDouble(tLa);
				
				String tLon = location[5];
				double thereLon = Double.parseDouble(tLon);
				
				String hLa = location[6];
				double hereLa = Double.parseDouble(hLa);
				
				String hLon = location[7];
				double hereLon = Double.parseDouble(hLon);
				
				loc.setAll(location[0], location[1], location[2], rating);
				loc.setThere(thereLa, thereLon);
				loc.setHere(hereLa, hereLon);
		//
		
		tvName = ( TextView ) findViewById ( R.id.tvName2 );
		tvVisits = ( TextView ) findViewById ( R.id.tvVisits2 );
		tvDist = ( TextView ) findViewById ( R.id.tvDistance2 );
		rbRating = ( RatingBar ) findViewById ( R.id.rbLikeness2 );
		btnGoMap = ( Button ) findViewById ( R.id.btnGoMap );
		btnUpdate = ( Button ) findViewById ( R.id.btnUpdate );		
		
		tvName.setText( loc.name );
		tvVisits.setText( loc.visits );
		tvDist.setText( loc.distance );
		rbRating.setRating( loc.rating );

		btnGoMap.setOnClickListener( this );
		btnUpdate.setOnClickListener( this );
		
		
		
		
		
		
		//
		
		mv = ( MapView ) findViewById ( R.id.mvMapView);
		mv.setBuiltInZoomControls( true );
		
		mc = mv.getController ( );
		mc.animateTo( loc.getGeoPointHere ( ) );
		mc.setZoom( 15 );
		
		// Location Marker
		MapOverlay mol = new MapOverlay ( );
		List<Overlay> listofOverlays = mv.getOverlays();
		listofOverlays.clear();
		listofOverlays.add(mol);
		
		mv.invalidate ( );
	}
    
	/**
	 * This method is to set RouteDisplay to true.
	 */
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return true;
	}
 
	/**
	 * This method handles what appropriate activity would be started 
	 * when any button on the screen is clicked.  
	 * @param v
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if ( v == btnGoMap )
		{
            Uri uri =Uri.parse("http://maps.google.com/maps?&saddr="+
            					loc.GetHereAsString( ) + "&daddr=" + 
            					loc.GetThereAsString( ) );
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
		}
		else if ( v == btnUpdate )
		{
			Intent intent = new Intent("com.wpi.iCoach.VISITINFO");
            startActivity(intent);
		}
	}

}
